package android.lei.block.activities;

import android.app.Activity;
import android.content.Intent;
import android.lei.block.AtyList;
import android.lei.block.MyAdapter;
import android.lei.block.R;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * 应用程序主界面，是所有功能界面的入口
 */
public class MainActivity extends AppCompatActivity {
    /**
     * 日志TAG
     */
    private static final String TAG = "MainActivity";
    /**
     * 显示数据的列表
     */
    private RecyclerView recyclerView;
    /**
     * 列表的适配器
     */
    private MyAdapter myAdapter;
    /**
     * 存放所有的Activity
     */
    private List<Map<String, Class>> activities;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /**
         * 首先需要获取所有的Activity列表
         */
        activities = AtyList.getAtyLists();

        /**
         * 初始化UI
         */
        initUI();
    }

    /**
     * 主页面UI初始化操作
     */
    private void initUI() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_content_main);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);

        /**
         * 添加默认分割线：高度为2px，颜色为灰色
         * mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL));
         * 添加自定义分割线：可自定义分割线drawable
         * mRecyclerView.addItemDecoration(new RecycleViewDivider(
         *     mContext, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));
         * 添加自定义分割线：可自定义分割线高度和颜色
         * mRecyclerView.addItemDecoration(new RecycleViewDivider(
         *     mContext, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.divide_gray_color)));
         */
//        recyclerView.addItemDecoration(new RecycleViewDivider(MainActivity.this, LinearLayoutManager.VERTICAL));

        myAdapter = new MyAdapter(this, activities);

        /**
         * 因为RecycleView没有点击事件，这里在Adapter中添加点击回调
         */
        myAdapter.setOnItemClickLitener(new MyAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(MainActivity.this, AtyList.CLASSES[position]);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        recyclerView.setAdapter(myAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
